var app = angular.module('BaseServer');

app.service('PublicFunction',['PublicConst', 'PublicEnum', 'ExtDialog', 
    function(PublicConst, PublicEnum, ExtDialog){
        return {
            successFun: function(res){
            },
            errFun: function(err){
                if(err.data.leval == 0) {
                    alert(err.data.title + ":\n" + err.data.message);
                }
                else {
                    alert(err.data.title + ":\n" + err.data.message);
                }
            },

            stringToDateTime: function (dateTime, dateFormat) {
                var returnDateTime;
                if(dateFormat == undefined){
                    dateFormat = PublicEnum.DateTimeFormat.DateTime;
                }

                if (dateTime.length == 0) {
                    return null;
                }

                dateTime = dateTime.replace(/^[^0-9]+|[^0-9]+$/g, '').replace(/[^0-9]+/g, "/");
                var dateNums = dateTime.split('/');
                if(dateNums.length < 3){
                    return null;
                }

                if(dateFormat == PublicEnum.DateTimeFormat.Date){
                    returnDateTime = new Date(dateNums[0], parseInt(dateNums[1]) - 1, dateNums[2]);
                }
                else {
                    var strHour = 0,
                        strMinute = 0,
                        strSecond = 0;

                    if(dateNums.length > 3){
                        strHour = dateNums[3];
                    }
                    if(dateNums.length > 4){
                        strMinute = dateNums[4];
                    }
                    if(dateNums.length > 5){
                        strSecond = dateNums[5];
                    }

                    returnDateTime = new Date(dateNums[0], parseInt(dateNums[1]) - 1, dateNums[2], strHour, strMinute, strSecond);
                }

                if(returnDateTime < PublicConst.DateTime.Min){
                    returnDateTime = PublicConst.DateTime.Min;
                }

                if(returnDateTime > PublicConst.DateTime.Max){
                    returnDateTime = PublicConst.DateTime.Max;
                }

                return returnDateTime;
            },
            dateTimeToString: function(dateTime, dateFormat){
                if(dateFormat == undefined){
                    dateFormat = PublicEnum.DateTimeFormat.DateTime;
                }

                if(dateTime == undefined || dateTime == null || dateTime <= PublicConst.DateTime.Min){
                    return '';
                }

                var returnDateTime,
                    strYear = dateTime.getFullYear().toString(),
                    strMonth, strDate, strHour, strMinute, strSecond;

                if(dateTime.getMonth() + 1 < 10)
                {
                    strMonth = "0" + (dateTime.getMonth() + 1).toString();
                }
                else{
                    strMonth = (dateTime.getMonth() + 1).toString();
                }

                if(dateTime.getDate() < 10)
                {
                    strDate = "0" + dateTime.getDate().toString();
                }
                else{
                    strDate = dateTime.getDate().toString();
                }

                returnDateTime = strYear + '.' + strMonth + '.' + strDate;

                if(dateFormat == PublicEnum.DateTimeFormat.DateTime){
                    if(dateTime.getHours() < 10)
                    {
                        strHour = "0" + dateTime.getHours().toString();
                    }
                    else{
                        strHour = dateTime.getHours().toString();
                    }

                    if(dateTime.getMinutes() < 10)
                    {
                        strMinute = "0" + dateTime.getMinutes().toString();
                    }
                    else{
                        strMinute = dateTime.getMinutes().toString();
                    }

                    if(dateTime.getSeconds() < 10)
                    {
                        strSecond = "0" + dateTime.getSeconds().toString();
                    }
                    else{
                        strSecond = dateTime.getSeconds().toString();
                    }

                    returnDateTime += ' ' + strHour + ':' + strMinute + ':' + strSecond;
                }

                return returnDateTime;
            },
            //获取当前月有多少天
            getMonthDays: function (dateTime) {
                var dt1 = new Date(dateTime.getFullYear(), dateTime.getMonth(), 1);
                var dt2 = new Date(dateTime.getFullYear(), dateTime.getMonth() + 1, 1);

                return (dt2.getTime() - dt1.getTime()) / (24 * 60 * 60 * 1000);
            },

            percentToNumber: function(val){
                return Number(val.replace('%', '')) / 100;
            },
            //获取文本框被选择的文本以及光标位置
            getSelectText: function(element) {
                var positionStart = element.selectionStart;
                var positionEnd = element.selectionEnd;
                var selectLen = positionEnd - positionStart
                var selectText = element.value.substr(positionStart, selectLen);

                return {
                    Start : positionStart,
                    End : positionEnd,
                    Length : selectLen,
                    Text : selectText
                };
            },

            loading: function (hide) {
                if (hide) {
                    ExtDialog.showLoading();
                } else {
                    ExtDialog.hideLoading();
                }
            },
            confirm: function(msg, title){
                if(confirm(msg)){
                    return true;
                }
                else{
                    return false;
                }
            },
            alert: function(msg, title) {
                ExtDialog.alert(msg, title);
            },
            tip: function(msg, title) {
                ExtDialog.tip(msg, title);
            },
            confirm: function(msg, title, options) {
                return ExtDialog.confirm(msg, title, options);
            },
            successFun: function(){
            },
            errFun: function(err){
                if(err.status != undefined){
                    var title = err.data.title;
                    var msg = err.data.message;
                    if(msg.indexOf('E11000') > -1){
                        msg = "主键索引重复"
                    }
                    ExtDialog.alert(msg, title);
                }
                else{
                    if(err.data.leval == 0){
                        ExtDialog.alert(err.data.message, err.data.title);
                    }
                    else{
                        ExtDialog.alert(err.data.message, err.data.title);
                    }
                }
            }
        };
    }
]);
